<?php

$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if (!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);

if (empty($this->Param['option'])) {
    
} else {
    switch ($this->Param['option']) {
        case 'list':
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('订单ID值错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
            
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            $where = array(
                '`up_oid` = ?' => $oid
            );
            $data = $OrderHelper->omList($where, $page, $rp, $this->Param);
            $json_data = array('total' => $data['count'], 'list' => array(), 'page' => $page, 'pagetotal' => $data['pagetotal']);
            if (!empty($data['all'])){
                foreach ($data['all'] AS $rowNum => $row) {
                    $row['up_info'] = unserialize($row['up_info']);
                    $json_data['list'][] = $row;
                }
            }
            $ApiHelper->logSave('omodify_list', $this->Param);
            ErrorMsg::ApiMsg('请求成功', 0, $json_data);
            break;
        case 'ajaxkey':
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('订单ID值错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
            
            $base = $this->Param['base'];
            $next = $this->Param['next'];
            $params = array(
                'oid' => $oid,
                'base' => $base,
                'next' => $next
            );
            $data = $OrderHelper->ajaxMinfo($params);
            $ApiHelper->logSave('order_ajaxkey', $this->Param);
            ErrorMsg::ApiMsg('请求成功', 0, $data);
            break;
        case 'save':
            $data['oid'] = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('订单ID值错误') : $this->Param['oid'];
            $data['base'] = empty($this->Param['base']) ? ErrorMsg::ApiMsg('基础修改参数错误') : $this->Param['base'];
            $data['next'] = empty($this->Param['next']) ? ErrorMsg::ApiMsg('基础修改参数错误') : $this->Param['next'];
            $data['base_val'] = is_null($this->Param['base_val']) ? ErrorMsg::ApiMsg('原值错误') : $this->Param['base_val'];
            $data['now_val'] = empty($this->Param['now_val']) ? ErrorMsg::ApiMsg('现值错误') : $this->Param['now_val'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($data['oid'], $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
//            $order_up_count = $OrderHelper->orderupCount(array(
//                '`up_oid` = ?' => $data['oid'],
//                '`up_status` = ?' => '申请中'
//            ));
//            if($order_up_count > 0){
//                ErrorMsg::ApiMsg('当前已经存在申请中的修改，审核后才能继续申请');
//            }
            
            $data['up_oid'] = $data['oid'];
            $data['up_uid'] = $this->Param['uid'];
            $member = $MemberHelper->getMember($this->Param['uid']);
            $data['up_uname'] = $member['u_realname'];
            $data['up_time'] = NOW_TIME;
            $data['up_reason'] = $this->Param['remarks'];
            $result = $OrderHelper->orderupSave($data);
            if($result['success'] == true){
                $ApiHelper->logSave('omodify_save', $this->Param);
                ErrorMsg::ApiMsg('保存成功', 0);
            }else{                
                ErrorMsg::ApiMsg($result['msg']);
            }
            break;
        default :
    }
}